package com.ljkj.cordial.chat.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.BaseActivity;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.SearchBean;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.nic.view.widget.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索结果加好友/加群组
 */
public class SearchResultActivity extends BaseActivity {

    @BindView(R.id.iv_avatar)
    CircleImageView ivAvatar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.ll_send_msg)
    FrameLayout llSendMsg;
    @BindView(R.id.tv_modify_remark)
    TextView tvModifyRemark;
    @BindView(R.id.ll_modify_remark)
    LinearLayout llModifyRemark;
    private int type;
    private SearchBean item;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_search_result;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        getToolbar().setTitle("");
        llModifyRemark.setVisibility(View.GONE);
        item = (SearchBean) getIntent().getSerializableExtra(Canstants.KEY_SEARCH_RESULT_INFO);
        type = getIntent().getIntExtra(Canstants.KEY_SEARCH_RESULT, Canstants.KEY_SEARCH_FRIEND);
        if (Canstants.KEY_SEARCH_FRIEND == type) {
            //加好友
            if (item != null) {
                tvName.setText(item.getName());
                if (!TextUtils.isEmpty(item.getUserId())) {
                    tvId.setText("" + item.getUserId());
                }
                if (!TextUtils.isEmpty(item.getTouristId())) {
                    tvId.setText("" + item.getTouristId());
                }
            }
            if (!TextUtils.isEmpty(item.getAvatars())) {
                AlbumManageUtil.getInstance().downLoadFileFriend(item.getAvatars(), tvId.getText().toString(), ivAvatar);
            }
        } else if (Canstants.KEY_SEARCH_GROUP == type) {
            //加群组
            if (item != null) {
                tvName.setText(item.getName());
                if (!TextUtils.isEmpty(item.getGroupId())) {
                    tvId.setText("" + item.getGroupId());
                }
            }
            if (!TextUtils.isEmpty(item.getAvatars())) {
                AlbumManageUtil.getInstance().downLoadFileFriend(item.getAvatars(), tvId.getText().toString(), ivAvatar);
            }
        }
    }

    public static void jumpTo(Activity activity, int type, SearchBean item) {
        Intent intent = new Intent(activity, SearchResultActivity.class);
        intent.putExtra(Canstants.KEY_SEARCH_RESULT, type);
        intent.putExtra(Canstants.KEY_SEARCH_RESULT_INFO, item);
        activity.startActivityForResult(intent, Canstants.REQUEST_CODE_BACK);
    }


    @OnClick({R.id.ll_send_msg, R.id.ll_modify_remark})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_send_msg:
                if (Canstants.KEY_SEARCH_FRIEND == type) {
                    //加好友
                    AddFriendActivity.jumpTo(this, type, item);
                } else if (Canstants.KEY_SEARCH_GROUP == type) {
                    //加群组
                    AddFriendActivity.jumpTo(this, type, item);
                }
                break;
            case R.id.ll_modify_remark:
                ModifyRemarkActivity.jumpTo(this, "修改备注名", ModifyRemarkActivity.OPTION_ALIAS_SEARCH);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Canstants.KEY_REMARK_RESULT == requestCode && Canstants.KEY_REMARK_RESULT == resultCode) {
            String remark = data.getStringExtra(Canstants.KEY_REMARK);
            tvName.setText(remark);
            return;
        }
        if (requestCode == Canstants.REQUEST_CODE_BACK && resultCode == Canstants.REQUEST_CODE_BACK) {
            setResult(Canstants.REQUEST_CODE_BACK);
            finish();
        }
    }
}
